package com.ruoyi.bizsys.service.impl;

import com.ruoyi.bizsys.domain.AwardActivity;
import com.ruoyi.bizsys.domain.AwardAgent;
import com.ruoyi.bizsys.mapper.AwardAgentMapper;
import com.ruoyi.bizsys.service.IAwardAgentService;
import com.ruoyi.bizsys.vo.AwardGroupVo;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.ObjectUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cyh
 * @date 2022/6/27 5:05 下午
 */
@Service
@Slf4j
@AllArgsConstructor
public class AwardAgentServiceImpl implements IAwardAgentService {

    private AwardAgentMapper awardAgentMapper;


    @Override
    public List<AwardAgent> list(AwardAgent awardAgent) {
        List<AwardAgent> list = awardAgentMapper.selectList(awardAgent);
        return list;
    }

    @Override
    public List<AwardGroupVo> fileParseList(MultipartFile file) throws Exception {
        log.info("fileParseList");
        String name = file.getOriginalFilename();
        if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
            throw new BusinessException("文件格式错误,请上传xlsx格式");
        }
        //解析文件
        List<AwardGroupVo> list = new ExcelUtil<>(AwardGroupVo.class).importExcel(file.getInputStream());
        if (!ObjectUtil.isNotEmpty(list)) {
            throw new BusinessException("提额名单是空文件");
        }
        if (list.size() > 200) {
            throw new BusinessException("操作数量不能大于200");
        }
        //过滤掉任意一个值为空的
        list = list.stream().filter(e->!Func.hasEmpty(e.getGroupCode(), e.getUserId())).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(list)){
            throw new BusinessException("导入数据为空");
        }
        log.info("fileParseList",list.size());
        return list;
    }


}
